<?php


namespace App\Http\Controllers\Api\Repositories;

use App\Jobs\AttachIpAddressJob;
use App\Wen\Utils\Settings;
use App\Models\WxSearch;

class WxSearchRepositores
{

    public static function record($uid,$search_content)
    {
        $first_model = WxSearch::where('user_id', $uid)->latest()->first();
        if($first_model){
            if($first_model->search_content == $search_content){
                return true;
            }
        }

        $SearchModel = new WxSearch();
        $SearchModel->user_id = $uid;
        $SearchModel->search_content = $search_content;

        global $__MINI_GLOBAL_IP__;
        if($__MINI_GLOBAL_IP__){
            $SearchModel->ip = $__MINI_GLOBAL_IP__;
        }

        $r = $SearchModel->save();

        if($r){
            if($__MINI_GLOBAL_IP__){
                AttachIpAddressJob::dispatch(4, $SearchModel->id)->delay(2);
            }
        }
        return $r;
    }

    public static function hot_search($num = 10, $field = ['search_content', 'is_hot']){
        if(Settings::get('app_hot_search', '')){
            return WxSearch::whereIn('id', explode(',', Settings::get('app_hot_search', '')))->limit($num)->get($field);
        }else{
            return WxSearch::where('is_hot', 1)->orderby('updated_at', 'desc')->limit($num)->get($field);
        }
    }

}
